#include "work.h"

void Pwm_Init(void)
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4|RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_7 | GPIO_Pin_6|GPIO_Pin_1;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	/*
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
	GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2 ,ENABLE);
	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
	*/
	
  TIM_InternalClockConfig(TIM4);
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitstructure4;
	TIM_TimeBaseInitstructure4.TIM_ClockDivision=TIM_CKD_DIV1 ;//fengping
	TIM_TimeBaseInitstructure4.TIM_CounterMode=TIM_CounterMode_Up  ;
	TIM_TimeBaseInitstructure4.TIM_Period=100-1;//ARR
	TIM_TimeBaseInitstructure4.TIM_Prescaler=7200-1;//psc
	TIM_TimeBaseInitstructure4.TIM_RepetitionCounter=0;//chongfujsqi
	TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitstructure4);

	TIM_InternalClockConfig(TIM3);
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitstructure3;
	TIM_TimeBaseInitstructure3.TIM_ClockDivision=TIM_CKD_DIV1 ;//fengping
	TIM_TimeBaseInitstructure3.TIM_CounterMode=TIM_CounterMode_Up  ;
	TIM_TimeBaseInitstructure3.TIM_Period=20000-1;//ARR
	TIM_TimeBaseInitstructure3.TIM_Prescaler=72-1;//psc
	TIM_TimeBaseInitstructure3.TIM_RepetitionCounter=0;//chongfujsqi
	TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitstructure3);
	
	TIM_OCInitTypeDef TIM_OCInitstructure3;
	TIM_OCStructInit(&TIM_OCInitstructure3);
	TIM_OCInitstructure3.TIM_OCMode=TIM_OCMode_PWM1 ;
	TIM_OCInitstructure3.TIM_OCPolarity=TIM_OCPolarity_High;
	TIM_OCInitstructure3.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitstructure3.TIM_Pulse=0;//CCR
	TIM_OC4Init(TIM3,&TIM_OCInitstructure3);
//	TIM_Cmd(TIM3, ENABLE);
	
	TIM_OCInitTypeDef TIM_OCInitstructure4;
	TIM_OCStructInit(&TIM_OCInitstructure4);
	TIM_OCInitstructure4.TIM_OCMode=TIM_OCMode_PWM1 ;
	TIM_OCInitstructure4.TIM_OCPolarity=TIM_OCPolarity_High;
	TIM_OCInitstructure4.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitstructure4.TIM_Pulse=0;//CCR
	TIM_OC1Init(TIM4,&TIM_OCInitstructure4);
	TIM_OC2Init(TIM4,&TIM_OCInitstructure4);
	TIM_OC3Init(TIM4,&TIM_OCInitstructure4);
	TIM_OC4Init(TIM4,&TIM_OCInitstructure4);
	TIM_Cmd(TIM4, ENABLE);
	
}
void PWM4_SetCompare1(uint16_t compare)
{
	TIM_SetCompare1(TIM4,compare);
	
}
void PWM4_SetCompare2(uint16_t compare)
{
	TIM_SetCompare2(TIM4,compare);
	
}
void PWM4_SetCompare3(uint16_t compare)
{
	TIM_SetCompare3(TIM4,compare);
}

void PWM4_SetCompare4(uint16_t compare)
{
	TIM_SetCompare4(TIM4,compare);
}
void PWM3_SetCompare4(uint16_t compare)
{
	TIM_SetCompare4(TIM3,compare);
}
void PWM_SetPrescaler(uint16_t Prescaler)
{
	TIM_PrescalerConfig(TIM4,Prescaler,TIM_PSCReloadMode_Immediate);
}
